package com.xiaohu.myspace.utils.filter;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
	private static final String DATE_FORMAT_REGULATION_EXPRESSION = "yyyy-MM-dd HH:mm:ss";
	private static final String DATE_FORMAT_REGULATION_EXPRESSION_TIME = "yyyy-MM-dd HH:mm";
	private static final String DATE_FORMAT_REGULATION_FORM = "yyyy-MM-dd";
	public static final String DATE_DIFF_TYPE_HOUR = "hour";
	public static final String DATE_DIFF_TYPE_DAY = "day";
	public static final String DATE_DIFF_TYPE_MIN = "min";
	public static final String DATE_DIFF_TYPE_SEC = "sec";

	public static Date stringToDate(String time, String type) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(type);
		try {
			Date data = dateFormat.parse(time);
			return data;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}

	public static String stringToStringWithType(String time, String type1,
			String type2) {
		SimpleDateFormat dateFormat1 = new SimpleDateFormat(type1);
		SimpleDateFormat dateFormat2 = new SimpleDateFormat(type2);
		try {
			Date data = dateFormat1.parse(time);
			return dateFormat2.format(data);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;
	}

	public static String getCurrentDateTimeStr(String formatExpression) {

		Date date = new Date();

		return formatDate(date, formatExpression);
	}

	public static String formatDate(Date date) {

		return formatDate(date, DATE_FORMAT_REGULATION_EXPRESSION);
	}

	public static String formatDateTime(Date date) {

		return formatDate(date, DATE_FORMAT_REGULATION_EXPRESSION_TIME);
	}

	public static String formatDate(Date date, String formatExpression) {

		if (null == formatExpression) {
			formatExpression = DATE_FORMAT_REGULATION_EXPRESSION;
		}

		SimpleDateFormat sf = new SimpleDateFormat(formatExpression);

		return null == date ? null : sf.format(date);
	}


	/**
	 * �園�頧砍�蝚虫葡
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateToStr(Object date, String pattern) {
		if (date == null)
			date = new Date();
		if (pattern == null)
			pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		try {
			return sf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String dateToStr(Date date, String pattern) {
		if (date == null)
			date = new Date();
		if (pattern == null)
			pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		try {
			return sf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 */
	public static String getCurrentDateTime(String date) {

		SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");

		Date dateTime = null;
		Calendar calendar = new GregorianCalendar();

		try {
			dateTime = sdfInput.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		calendar.setTime(dateTime);
		return formatDate(dateTime, DATE_FORMAT_REGULATION_FORM);
	}

}
